package com.soap.desensitize.strategy.irreversible;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.soap.desensitize.encry.irreversible.AbstractIRreversibleEncry;
import com.soap.desensitize.exception.strategy.EncodeException;

/**
 * @desc 	MD5策略类
 * @author 	高增源
 * @time	2022-04-20
 */

public class Md5Strategy extends AbstractIRreversibleEncry<String> {
	private static Md5Strategy instance = null;
	private Md5Strategy(){}

	/**
	 * @desc 获得实例
	 * @return
	 */
	public static synchronized Md5Strategy getInstance(){
		if(instance == null){
			instance = new Md5Strategy();
		}
		return instance;
	}
	@Override
	public String encode(String data) throws EncodeException {
		MessageDigest md5 = null;//申明使用MD5算法
		try {
			md5 = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			throw new EncodeException("加密错误，加密算法不存在");
		}
		md5.update(data.getBytes());
        String result = byte2str(md5.digest());
		return result;
	}

}
